import store from "../store";
import axios from 'axios';
import { ElMessage } from 'element-plus';

axios.defaults.baseURL = '/api';

export const utilMethods = {
    detail(goods) {
        let search = "?";
        search += "goods=" + JSON.stringify(goods);

        let type = {};
        axios.get("/type/" + goods.tid)
            .then(result => {
                if (result.data.code === 1) {
                    type = result.data.data;
                } else {
                    ElMessage.info("错误提示" + result.data.msg);
                }
            })
            .catch(err => {
                console.log(err);
                if (err.response.status === 404) {
                    ElMessage.error("无法连接到后端服务器");
                    return;
                }
                ElMessage.error("请求异常：" + err);
            })
            .finally(() => {
                search += "&type=" + JSON.stringify(type);
                localStorage.setItem('store', JSON.stringify(store.state));
                window.open("/detail" + search);
            })



    },
    debounce(func, wait = 500) {
        let timeout;
        if (timeout) {
            clearTimeout(timeout);
        }
        timeout = setTimeout(func, wait);
    }
}